﻿using System.Collections;
using System.Collections.Generic;
using System.Data;
using UnityEngine;
using UnityEngine.UI;

public class Store : MonoBehaviour
{
    Text textGold;//金币文本
    Text textJewel;//钻石文本

    private void Start()
    {
        textGold = GameObject.Find("Canvas").transform.Find("个人信息/金币数值").GetComponent<Text>();
        textJewel = GameObject.Find("Canvas").transform.Find("个人信息/钻石数值").GetComponent<Text>();
        //打开数据库
        MySqlAccess.OpenSql();
        MySqlAccess.gold = MySqlAccess.QueryGold();
        MySqlAccess.jewel = MySqlAccess.QueryJewel();
        BuyJewel();
        BuyIQ();
    }

    //买钻石
    public void BuyJewel()
    {
        GameObject.Find("Canvas").transform.Find("商店面板/兑换钻石").GetComponent<Button>().onClick.AddListener(delegate
        {
            if (MySqlAccess.gold >= 100)
            {
                MySqlAccess.gold -= 100;
                MySqlAccess.UpdateInto("shop", new string[] { "gold" }, new string[] { MySqlAccess.gold.ToString() }, "id", LoginPanel.Showtetx());
                textGold.text = MySqlAccess.QueryGold().ToString();
                MySqlAccess.jewel += 1;
                MySqlAccess.UpdateInto("shop", new string[] { "jewel" }, new string[] { MySqlAccess.jewel.ToString() }, "id", LoginPanel.Showtetx());
                textJewel.text = MySqlAccess.QueryJewel().ToString();
            }
            else
            {
                Debug.Log("金币不足");
            }
        });
    }

    //买知识
    public void BuyIQ()
    {
        GameObject.Find("Canvas").transform.Find("商店面板/知识升级").GetComponent<Button>().onClick.AddListener(delegate
        {
            if (MySqlAccess.jewel >= 5)
            {
                MySqlAccess.jewel -= 5;
                MySqlAccess.UpdateInto("shop", new string[] { "jewel" }, new string[] { MySqlAccess.jewel.ToString() }, "id", LoginPanel.Showtetx());
                textJewel.text = MySqlAccess.QueryJewel().ToString();
            }
            else
            {
                Debug.Log("钻石不足");
            }
        });
    }
}

